package com.bijava.util;

import org.apache.log4j.Logger;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;

public class Base64Util {
	
	public static Logger log = Logger.getLogger(Base64Util.class);
	
	/**
	 * 加密
	 * @param str
	 * @return
	 */
    public static String encode(String str) {
        byte[] b = null;  
        String s = null;
        try {  
            b = str.getBytes("utf-8");  
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();  
        }  
        if (b != null) {  
            s = new BASE64Encoder().encode(b);
        }  
        return s;  
    }  
    
    /**
	 * 加密
	 * @param str
	 * @return
	 */
    public static String encode(byte[] b) {
    	String str = "";
        if (b != null) {  
        	str = new BASE64Encoder().encode(b);
        }  
        return str;  
    }  
  
    /**
     * 解密
     * @param s
     * @return
     */
    public static String decode(String s) {
        byte[] b = null;  
        String result = null;
        if (s != null) {  
            BASE64Decoder decoder = new BASE64Decoder();
            try {  
                b = decoder.decodeBuffer(s);  
                result = new String(b, "utf-8");
            } catch (Exception e) {
                e.printStackTrace();  
            }  
        }  
        return result;  
    }

    /**
     * 解密为字节数组
     * @param s 加密过的Base64字符串
     * @return 解密后的字节数组
     */
    public static byte[] decodeToByte(String s){
        byte[] result = null;
        if(s != null){
            BASE64Decoder decoder = new BASE64Decoder();
            try{
                result = decoder.decodeBuffer(s);
                for(int i=0;i<result.length;++i){
                    if(result[i] < 0){
                        result[i] += 256;
                    }
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return result;
    }

    /**
     * 根据照片的Url将其转换为Base64字符串
     * @param imgUrl 照片的Url
     * @return Base64字符串
     */
    public static String imgUrlToBase64(String imgUrl){
        String result = null;
        try{
            if(imgUrl != null){
                URL url = new URL(imgUrl);
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                conn.setRequestMethod("GET");
                conn.setConnectTimeout(5*1000);
                InputStream inputStream = conn.getInputStream();
                ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
                byte[] buffer = new byte[1024];
                int len = 0;
                while((len = inputStream.read(buffer)) != -1){
                    outputStream.write(buffer,0,len);
                }
                inputStream.close();
                byte[] data = outputStream.toByteArray();
                BASE64Encoder encoder = new BASE64Encoder();
                result = encoder.encode(data);
            }
        } catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }
    
    public static void main(String[] args) {
//    	String test = "测试";
//    	String base64Content = Base64Util.encode(test);
//    	log.info("base64Content:"+base64Content);
//    	String result = Base64Util.decode(base64Content);
//    	log.info("result:"+result);
    }
}
